package servlet;

import java.io.IOException;
import org.bson.Document;
import org.bson.types.ObjectId;

import com.mongodb.client.MongoCursor;

import mongo.TeachingMaterialsType;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(description = "teachingMaterialsTypeServletProcess", urlPatterns = { "/manager/TeachingMaterialsType/teachingMaterialsTypeServlet" })
public class teachingMaterialsTypeServlet extends HttpServlet{
	private TeachingMaterialsType TeachingMaterialsType = new TeachingMaterialsType();
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
	    response.setContentType("text/html;charset=UTF-8");
		
		String method = request.getParameter("method");
		if(method.equals("add")){
			this.add(request, response);
		}else if(method.equals("delete")){
			this.delete(request, response);
		}else if(method.equals("list")){
			this.list(request, response);
		}else{
//			request.getRequestDispatcher("404.jsp").forward(request, response); 添加错误页面
		}
	
	}
	
	
	public void add(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		Boolean result = null ;
		try{
			    String id = request.getParameter("id");
				String name = request.getParameter("name");
				String explain = request.getParameter("explain");
				Boolean ishand = Boolean.parseBoolean(request.getParameter("ishand"));
				String mark = request.getParameter("mark");
				Document newDocument = new Document("name",name).
						append("ishand", ishand).
						append("explain", explain).
						append("mark", mark).append("id", id);
				if(TeachingMaterialsType.isExist(id)){
					//ID存在，不允许添加
					request.setAttribute("error","材料类型ID已存在！");
					request.getRequestDispatcher("addTeachingMaterialsType.jsp").forward(request, response);
				}else{
					result = TeachingMaterialsType.add(newDocument);
					if(result)
						this.list(request, response);
				}
			//TODO针对返回结果和所属模块，返回到相对应的页面
		}catch(Exception e){
            System.err.println( e.getClass().getName() + ": " + e.getMessage() );
            //TODO发生异常,跳转到对应页面
        }
		
	}
	
	public void delete(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
        try {
    		ObjectId _id = new ObjectId(request.getParameter("_id"));
    		Boolean result = TeachingMaterialsType.delete(_id);
    		if(result)
				this.list(request, response);
    		//TODO针对返回结果和所属模块，返回到相对应的页面  
        }catch(Exception e){
            System.err.println( e.getClass().getName() + ": " + e.getMessage() );
          //TODO发生异常,跳转到对应页面
        }
	}
	
	
	public void list(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
        try {
    		MongoCursor<Document> result = TeachingMaterialsType.list();
    		request.setAttribute("result", result);
    		request.getRequestDispatcher("teachingMaterialsTypeList.jsp").forward(request, response);
    		//response.sendRedirect("departList.jsp");
    		//TODO针对所属模块，返回到相对应的页面   
        }catch(Exception e){
            System.err.println( e.getClass().getName() + ": " + e.getMessage() );
          //TODO发生异常,跳转到对应页面
        }

	}
	
}
